package com.example.mallproduct.controller;

import com.example.mallcommon.RestResult;
import com.example.mallproduct.api.domain.Product;
import com.example.mallproduct.api.service.ProductService;
import com.example.mallproduct.feign.ProductFeign;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/** @author yangtao RefreshScope 在需要动态读取配置的类上添加此注解就可以实现动态刷新功能 */
@Api(tags = "product模块")
@RestController
@RefreshScope
public class ProductController implements ProductFeign {
  @Autowired private ProductService productService;

  @Value("${server.port}")
  private String port;

  @ApiOperation(value = "根据产品编码查找对应的产品")
  @ApiImplicitParam(name = "pid",value = "产品编码", required = true,paramType = "path")
  @ApiResponses({
          @ApiResponse(code=400,message="请求参数没填好"),
          @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
  })
  @Override
  @RequestMapping(value = "/product/{pid}",method = RequestMethod.GET)
  public RestResult<Product> findByPid(@PathVariable("pid") Integer pid) {
    Product product = productService.findByPid(pid);
    System.out.println("实例被调用，端口号为：" + port);
    return RestResult.success(product);
  }

  @ApiOperation(value = "根据产品编码减少对应的产品的库存")

  @ApiImplicitParams({
      @ApiImplicitParam(name = "pid",value = "产品编码", required = true,paramType = "query"),
      @ApiImplicitParam(name = "num",value = "购买量", required = true,paramType = "query")
  })
  @ApiResponses({
          @ApiResponse(code=400,message="请求参数没填好"),
          @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
  })
  @Override
  @RequestMapping(value = "/product/reduceInventory",method = RequestMethod.GET)
  public void reduceInventory(@RequestParam("pid") Integer pid, @RequestParam("num") int num) {
    productService.reduceInventory(pid, num);
  }
}
